package com.ali.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private static final ZoneId zoneId = ZoneId.systemDefault();
    private static final ZoneOffset zoneOffset = ZoneOffset.ofHours(8);
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter dateTimeFormatter_1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter dateTimeFormatter_2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final DateTimeFormatter dateTimeFormatter_3 = DateTimeFormatter.ofPattern("HH:mm:ss");

//Date

    /**
     * 比较
     */
    public static int compareTo(Date date1, Date date2) {
        return date1.compareTo(date2);
    }

//Calendar

    /**
     * 获取年
     */
    public static int getYear(Calendar calendar) {
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取月
     */
    public static int getMonth(Calendar calendar) {
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取日
     */
    public static int getDay(Calendar calendar) {
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取时
     */
    public static int getHour(Calendar calendar) {
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取分
     */
    public static int getMinute(Calendar calendar) {
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取秒
     */
    public static int getSecond(Calendar calendar) {
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 设置
     */
    public static void set(Calendar calendar, int year, int month, int day, int hour, int minute, int second) {
        calendar.set(year, month, day, hour, minute, second);
    }

    /**
     * 加年
     */
    public static void addYear(Calendar calendar, int year) {
        calendar.add(Calendar.YEAR, year);
    }

    /**
     * 加月
     */
    public static void addMonth(Calendar calendar, int month) {
        calendar.add(Calendar.MONTH, month);
    }

    /**
     * 加日
     */
    public static void addDay(Calendar calendar, int day) {
        calendar.add(Calendar.DAY_OF_MONTH, day);
    }

    /**
     * 加时
     */
    public static void addHour(Calendar calendar, int hour) {
        calendar.add(Calendar.HOUR_OF_DAY, hour);
    }

    /**
     * 加分
     */
    public static void addMinute(Calendar calendar, int minute) {
        calendar.add(Calendar.MINUTE, minute);
    }

    /**
     * 加秒
     */
    public static void addSecond(Calendar calendar, int second) {
        calendar.add(Calendar.SECOND, second);
    }

    /**
     * 比较
     */
    public static int compareTo(Calendar calendar1, Calendar calendar2) {
        return calendar1.compareTo(calendar2);
    }

//LocalDateTime

    /**
     * 获取年
     */
    public static int getYear(LocalDateTime localDateTime) {
        return localDateTime.getYear();
    }

    /**
     * 获取月
     */
    public static int getMonth(LocalDateTime localDateTime) {
        return localDateTime.getMonth().getValue();
    }

    /**
     * 获取日
     */
    public static int getDay(LocalDateTime localDateTime) {
        return localDateTime.getDayOfMonth();
    }

    /**
     * 获取时
     */
    public static int getHour(LocalDateTime localDateTime) {
        return localDateTime.getHour();
    }

    /**
     * 获取分
     */
    public static int getMinute(LocalDateTime localDateTime) {
        return localDateTime.getMinute();
    }

    /**
     * 获取秒
     */
    public static int getSecond(LocalDateTime localDateTime) {
        return localDateTime.getSecond();
    }

    /**
     * 加年
     */
    public static LocalDateTime addYear(LocalDateTime localDateTime, int year) {
        return localDateTime.plusYears(year);
    }

    /**
     * 加月
     */
    public static LocalDateTime addMonth(LocalDateTime localDateTime, int month) {
        return localDateTime.plusMonths(month);
    }

    /**
     * 加日
     */
    public static LocalDateTime addDay(LocalDateTime localDateTime, int day) {
        return localDateTime.plusDays(day);
    }

    /**
     * 加时
     */
    public static LocalDateTime addHour(LocalDateTime localDateTime, int hour) {
        return localDateTime.plusHours(hour);
    }

    /**
     * 加分
     */
    public static LocalDateTime addMinute(LocalDateTime localDateTime, int minute) {
        return localDateTime.plusMinutes(minute);
    }

    /**
     * 加秒
     */
    public static LocalDateTime addSecond(LocalDateTime localDateTime, int second) {
        return localDateTime.plusSeconds(second);
    }

    /**
     * 减年
     */
    public static LocalDateTime minusYear(LocalDateTime localDateTime, int year) {
        return localDateTime.minusYears(year);
    }

    /**
     * 减月
     */
    public static LocalDateTime minusMonth(LocalDateTime localDateTime, int month) {
        return localDateTime.minusMonths(month);
    }

    /**
     * 减日
     */
    public static LocalDateTime minusDay(LocalDateTime localDateTime, int day) {
        return localDateTime.minusDays(day);
    }

    /**
     * 减时
     */
    public static LocalDateTime minusHour(LocalDateTime localDateTime, int hour) {
        return localDateTime.minusHours(hour);
    }

    /**
     * 减分
     */
    public static LocalDateTime minusMinute(LocalDateTime localDateTime, int minute) {
        return localDateTime.minusMinutes(minute);
    }

    /**
     * 减秒
     */
    public static LocalDateTime minusSecond(LocalDateTime localDateTime, int second) {
        return localDateTime.minusSeconds(second);
    }

    /**
     * 比较
     */
    public static boolean isBefore(LocalDateTime localDateTime1, LocalDateTime localDateTime2) {
        return localDateTime1.isBefore(localDateTime2);
    }

    /**
     * 比较
     */
    public static boolean isAfter(LocalDateTime localDateTime1, LocalDateTime localDateTime2) {
        return localDateTime1.isAfter(localDateTime2);
    }

//LocalDate

    /**
     * 获取年
     */
    public static int getYear(LocalDate localDate) {
        return localDate.getYear();
    }

    /**
     * 获取月
     */
    public static int getMonth(LocalDate localDate) {
        return localDate.getMonth().getValue();
    }

    /**
     * 获取日
     */
    public static int getDay(LocalDate localDate) {
        return localDate.getDayOfMonth();
    }

    /**
     * 加年
     */
    public static LocalDate addYear(LocalDate localDate, int year) {
        return localDate.plusYears(year);
    }

    /**
     * 加月
     */
    public static LocalDate addMonth(LocalDate localDate, int month) {
        return localDate.plusMonths(month);
    }

    /**
     * 加日
     */
    public static LocalDate addDay(LocalDate localDate, int day) {
        return localDate.plusDays(day);
    }

    /**
     * 减年
     */
    public static LocalDate minusYear(LocalDate localDate, int year) {
        return localDate.minusYears(year);
    }

    /**
     * 减月
     */
    public static LocalDate minusMonth(LocalDate localDate, int month) {
        return localDate.minusMonths(month);
    }

    /**
     * 减日
     */
    public static LocalDate minusDay(LocalDate localDate, int day) {
        return localDate.minusDays(day);
    }

    /**
     * 比较
     */
    public static boolean isBefore(LocalDate localDate1, LocalDate localDate2) {
        return localDate1.isBefore(localDate2);
    }

    /**
     * 比较
     */
    public static boolean isAfter(LocalDate localDate1, LocalDate localDate2) {
        return localDate1.isAfter(localDate2);
    }

//LocalTime

    /**
     * 获取时
     */
    public static int getHour(LocalTime localTime) {
        return localTime.getHour();
    }

    /**
     * 获取分
     */
    public static int getMinute(LocalTime localTime) {
        return localTime.getMinute();
    }

    /**
     * 获取秒
     */
    public static int getSecond(LocalTime localTime) {
        return localTime.getSecond();
    }

    /**
     * 加时
     */
    public static LocalTime addHour(LocalTime localTime, int hour) {
        return localTime.plusHours(hour);
    }

    /**
     * 加分
     */
    public static LocalTime addMinute(LocalTime localTime, int minute) {
        return localTime.plusMinutes(minute);
    }

    /**
     * 加秒
     */
    public static LocalTime addSecond(LocalTime localTime, int second) {
        return localTime.plusSeconds(second);
    }

    /**
     * 减时
     */
    public static LocalTime minusHour(LocalTime localTime, int hour) {
        return localTime.minusHours(hour);
    }

    /**
     * 减分
     */
    public static LocalTime minusMinute(LocalTime localTime, int minute) {
        return localTime.minusMinutes(minute);
    }

    /**
     * 减秒
     */
    public static LocalTime minusSecond(LocalTime localTime, int second) {
        return localTime.minusSeconds(second);
    }

    /**
     * 比较
     */
    public static boolean isBefore(LocalTime localTime1, LocalTime localTime2) {
        return localTime1.isBefore(localTime2);
    }

    /**
     * 比较
     */
    public static boolean isAfter(LocalTime localTime1, LocalTime localTime2) {
        return localTime1.isAfter(localTime2);
    }

//Timestamp

    /**
     * 获取当前时间戳
     */
    public static long getCurrentTimestamp() {
        return System.currentTimeMillis();
    }

//Date与Calendar

    /**
     * Date转化为Timestamp
     */
    public static Calendar dateToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * Calendar转化为Date
     */
    public static Date calendarToDate(Calendar calendar) {
        return calendar.getTime();
    }

//Date与LocalDateTime

    /**
     * Date转化为LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), zoneId);
    }

    /**
     * LocalDateTime转化为Date
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        return Date.from(zonedDateTime.toInstant());
    }

//Date与Timestamp

    /**
     * Date转化为Timestamp
     */
    public static long dateToTimestamp(Date date) {
        return date.getTime();
    }

    /**
     * Timestamp转化为Date
     */
    public static Date timestampToDate(long timestamp) {
        Date date = new Date();
        date.setTime(timestamp);
        return date;
    }

//Date与String

    /**
     * Date转化为String
     */
    public static String dateToString(Date date) {
        return simpleDateFormat.format(date);
    }

    /**
     * String转化为Date
     */
    public static Date stringToDate(String dateStr) {
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

//Calendar与LocalDateTime

    /**
     * Calendar转化为LocalDateTime
     */
    public static LocalDateTime calendarToLocalDateTime(Calendar calendar) {
        return LocalDateTime.ofInstant(calendar.toInstant(), zoneId);
    }

//Calendar与Timestamp

    /**
     * Calendar转化为Timestamp
     */
    public static long calendarToTimestamp(Calendar calendar) {
        return calendar.getTimeInMillis();
    }

    /**
     * Timestamp转化为Calendar
     */
    public static Calendar timestampToCalendar(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        return calendar;
    }

//LocalDateTime与LocalDate

    /**
     * LocalDateTime转化为LocalDate
     */
    public static LocalDate localDateTimeToLocalDate(LocalDateTime localDateTime) {
        return localDateTime.toLocalDate();
    }

    /**
     * LocalDate转化为LocalDateTime
     */
    public static LocalDateTime localDateToLocalDateTime(LocalDate localDate, int hour, int minute, int second) {
        return localDate.atTime(hour, minute, second);
    }

//LocalDateTime与LocalTime

    /**
     * LocalDateTime转化为LocalTime
     */
    public static LocalTime localDateTimeToLocalTime(LocalDateTime localDateTime) {
        return localDateTime.toLocalTime();
    }

//LocalDateTime与Timestamp

    /**
     * LocalDateTime转化为Timestamp
     */
    public static long localDateTimeToTimestamp(LocalDateTime localDateTime) {
        Instant instant = localDateTime.toInstant(zoneOffset);
        return instant.toEpochMilli();
    }

    /**
     * Timestamp转化为LocalDateTime
     */
    public static LocalDateTime timestampToLocalDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZonedDateTime zonedDateTime = instant.atZone(zoneOffset);
        return zonedDateTime.toLocalDateTime();
    }

//LocalDateTime与String

    /**
     * LocalDateTime转化为String
     */
    public static String localDateTimeToString(LocalDateTime localDateTime) {
        return dateTimeFormatter_1.format(localDateTime);
    }

    /**
     * String转化为LocalDateTime
     */
    public static LocalDateTime stringToLocalDateTime(String dateStr) {
        return LocalDateTime.parse(dateStr, dateTimeFormatter_1);
    }

//LocalDate与String

    /**
     * LocalDate转化为String
     */
    public static String localDateToString(LocalDate localDate) {
        return dateTimeFormatter_2.format(localDate);
    }

    /**
     * String转化为LocalDate
     */
    public static LocalDate stringToLocalDate(String dateStr) {
        return LocalDate.parse(dateStr, dateTimeFormatter_2);
    }

//LocalTime与String

    /**
     * LocalTime转化为String
     */
    public static String localTimeToString(LocalTime localTime) {
        return dateTimeFormatter_3.format(localTime);
    }

    /**
     * String转化为LocalTime
     */
    public static LocalTime stringToLocalTime(String dateStr) {
        return LocalTime.parse(dateStr, dateTimeFormatter_3);
    }
}